home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-04-27 | 2.2 KB | 50 lines | [TEXT/GEOL] |
- Item 9255980 24-April-90 17:46PDT
-
- From: ISAACSON Isaacson, Trygve
-
- To: CPLUS.DEV$ C++ Interest List--Developers
- CPLUS.APPLE$ C++ Interest List--Apple Employees
-
- Sub: Constructor Hell
-
- Attn: CPLUS.DEV$
- Attn: CPLUS.APPLE$
- SentBy: Trygve Isaacson
- Date 4/24/90
- Subject Constructor Hell
- From Trygve Isaacson
- To CPLUS.APPLE$, CPLUS.DEV$
-
- REGARDING Constructor Hell
- Don, I see three basic ways to "solve" the problems with constuctor failure.
-
- One approach is to do essentially what your example showed: use a validation
- routine that checks to see if the constructor succeeded. Note however that
- the example code, as is, is potentially deadly. If the 'free store instance'
- can't be allocated, p will be nil and the call to 'p->Validate()' may die a
- horrible death or may return true!
-
- A second approach, which I think is better, is to NOT do anything in the
- constructor; actually, I guess that means 'don't use constructors.' Use the
- Validate method to do the initialization and validation, and return a success
- or failure indicator (a Boolean, or an error code). I like the MacApp
- convention of naming the init method (it would be IAllocatingClass in your
- example). One problem with constructors is the syntax, which basically
- requires that if you create a subclass, the constructor parameters must be a
- superset of the parent class' constructor parameters. By using an explicit
- init method, you can define its parameter list however you want (maybe your
- subclass takes no init parameters, because it simply specializes the init
- parameters to the parent class).
-
- A third approach is, as you suggested, to use an exception-handling mechanism.
- I think that the 'don't use constructors' rule can still apply here. This is
- what MacApp does. If you're using MacApp, this is great; I think you can even
- use MacApp's exception handling for non-PascalObjects. If you're not using
- MacApp, a similar mechanism could be implemented. If you look at the source
- code to UFailure, you'll see that MacApp's exception-handling mechanism is
- actually quite straightforward. And it works well.
-
- Not a Fan of Constructors,
- Trygve Isaacson
-
-